#include "rougetradersgui_two.h"
#include "Session.h"
#include "Market.h"
#include "Player.h"
#include <string>
#include <iostream>
#include <map>
#include <Qt>
#include <QInputDialog>
#include <QDir>
#include <QListWidget>
#include <QString>
#include <QTableWidget>

using namespace std;

string Session::state_names[5] = {"VIC","NSW","SA","QLD","WA"};

Session::Session()
{
	day = 1;
	max_days = 30;
	running = true;
	total_states = 5;
	current_state_index = 0;
}

void Session::print_state_names(void) { cout << "PRINTING STATE_NAME[0]: " << state_names[0] << endl;}

int Session::get_total_states(void) { return total_states; }

void Session::start_game(void)
{

	//init_players("test");
	init_market();		
	init_states();
	
	//	days_choice();
	//	for (day;day<=max_days;day++)
	//	{
	//	// initialise states 
	//	// for each state initialise a market
	//		int day_choice = day_menu();
	//		if (day_choice == 1)
	//		{
	//			// change city
	//			change_city();
	//		}
	//		else
	//		{
	//			// trade
	//			trade();
	//		}
	//	// Menu
	//	// Market rates 
	//	// Travel options
	//	// Specific state option

	//	//Market new_market = Market::Market();
	//	}
	//}
	//else { running = false; }
}

void Session::trade(void)
{
	// display market for that state
	cout << "---------- Trading --------------" << endl;
	cout << current_state_index << endl;
	cout << states[current_state_index].get_name() << endl;
	int feedback;
	feedback = states[current_state_index].print_market();
}

int Session::day_menu(void)
{
	return 1;
}

void Session::change_city(void)
{
	cout << "Choose city:" << endl;
	int city_choice;
	for (int x=0; x < get_total_states(); x++)
	{
		cout << x << ")  - " << states[x].get_name() << endl;
	}
	cin >> city_choice;
	switch (city_choice)
		{
		case 0: 
			{
				cout << "Moving to " << states[0].get_name() << endl;
				current_state_index = 0;
				break;
			}
		case 1: 
			{
				cout << "Moving to " << states[1].get_name() << endl;
				current_state_index = 1;
				break;
			}
		case 2: 
			{
				cout << "Moving to " << states[2].get_name() << endl;
				current_state_index = 2;
				break;
			}
		case 3: 
			{
				cout << "Moving to " << states[3].get_name() << endl;
				current_state_index = 3;
				break;
			}
		case 4: 
			{
				cout << "Moving to " << states[4].get_name() << endl;
				current_state_index = 4;
				break;
			}

		default: 
			{
				cout << "Not a valid input... try again" << endl;
				change_city();
				break;
			}
		}
}

void Session::days_choice(void)
{
	int days;
	cout << "How many days do you want the game to go for?" << endl;
	while(! (cin >> days) )
	{
		cout << " This is not a number. Enter the amount of days you want:" << endl;
		cin.clear();
		cin.ignore(1000, '\n');
	}
	set_max_days(days);
	days_print();
}

void Session::set_max_days(int x) { max_days = x; }

void Session::days_print() { cout << "Start dealing for " << max_days << " days!" << endl; }

void Session::init_player(string player_name)
{
	Player new_player = Player(player_name);
	current_player = new_player;
}

void Session::init_market()
{
	/*  This is where we set the market prices */
	cout << "initialise market..." << endl;
	Session::set_market["Car"] = 50000.0;
	Session::set_market["TV"] = 950.0;
	Session::set_market["Computer"] = 1200.0;
	Session::set_market["Shoes"] = 150.0;
	Session::set_market["Watches"] = 100.0;
	Session::set_market["Stereo"] = 23.50;
	Session::set_market["Motorbike"] = 18750.0;
	Session::set_market["Coffee machine"] = 210.0;
}

void Session::init_states(void)
{
	map<string,float>* pSetMarket = &set_market;
//
//	for (int x=0; x < get_total_states(); x++)
//	{
//		QString qstr = QString::fromStdString(state_names[x]);
//		new QListWidgetItem(qstr, qt_app->ui.listWidgetCity, 0);
//
//		State new_state = State(state_names[x]);
//		new_state.set_local_market(pSetMarket);
//		states[x] = new_state;
//	}
//	set_market_current_city();
}

void Session::set_market_current_city(void)
{
	//for (int i = 0; i < 8; i++)
	//{	
	//	QString qstr = QString::fromStdString(this->states[current_state_index].get_market_index(i).get_name());
	//	QTableWidgetItem *nameItem = new QTableWidgetItem();
	//	nameItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
	//	nameItem->setFlags(Qt::ItemIsSelectable);
	//	nameItem->setText(qstr);

	//	QString q_current_cash = QString::number(this->states[current_state_index].get_market_index(i).get_price(), 'g', 5);
	//	QTableWidgetItem *priceItem = new QTableWidgetItem();
	//	priceItem->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable);
	//	priceItem->setText(q_current_cash);

	//	int row = qt_app->ui.tableWidgetMarket->rowCount();

	//	qt_app->ui.tableWidgetMarket->insertRow(row);
	//	qt_app->ui.tableWidgetMarket->setItem(row, 0, nameItem);
	//	qt_app->ui.tableWidgetMarket->setItem(row, 1, priceItem);
	//}
	//qt_app->ui.label_City->setText(QString::fromStdString(current_state_name()));
}

void Session::change_cities(void)
{
	//int index;
	//index = qt_app->ui.listWidgetCity->currentRow();
	//current_state_index = index;
	//set_market_current_city();
}

void Session::print_states(void)
{
	//cout << "printing states:" << endl;
	for (int y=0; y < get_total_states(); y++)
	{
		cout << "State:" <<  states[y].get_name() << endl;
		//cout << "State: " << states[y].get_name() << endl;
	}
}

string Session::current_state_name(void)
{
	return this->states[current_state_index].get_name();
}

Session::~Session(void)
{
}
